package hsl.study.videocapturesystem.controller;


import hsl.study.videocapturesystem.service.MultiModelService;
import hsl.study.videocapturesystem.service.impl.MultiModelServiceImpl;
import org.springframework.context.event.EventListener;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description:
 * @Author: hsl
 * @CreateDate: 2025/8/5 11:10
 * @Version: 1.0
 */

@RestController
@RequestMapping("/multiModel")
public class MultiModelController {

    @Resource
    private MultiModelService multiModelService;

    // 存储最新结果的缓存
    private final Map<String, Object> latestResult = new ConcurrentHashMap<>();

    /**
     * 监听AI结果事件
     */
    @EventListener
    public void handleAIResult(MultiModelServiceImpl.AIResultEvent event) {
        // 存储最新结果
        latestResult.putAll(event.getResultData());

        // 这里可以添加WebSocket推送逻辑
        // webSocketService.sendToAll(event.getResultData());
    }

    // 获取最新结果的API
    @GetMapping("/latest-result")
    public Map<String, Object> getLatestResult() {
        return latestResult;
    }
    /**
     * 启动视频捕获和AI处理服务
     * @return 启动结果消息
     */
    @PostMapping("/start")
    public ResponseEntity<String> startCapture() {
        try {
            // 调用服务层启动方法
            multiModelService.startProcessing();
            return ResponseEntity.ok("视频捕获和AI处理服务已启动");
        } catch (Exception e) {
            return ResponseEntity.internalServerError()
                    .body("启动失败: " + e.getMessage());
        }
    }


    /**
     * 停止视频捕获和AI处理服务
     * @return 停止结果消息
     */
    @PostMapping("/stop")
    public ResponseEntity<String> stopCapture() {
        try {
            // 调用服务层停止方法
            multiModelService.stopProcessing();
            return ResponseEntity.ok("视频捕获和AI处理服务已停止");
        } catch (Exception e) {
            return ResponseEntity.internalServerError()
                    .body("停止失败: " + e.getMessage());
        }
    }
}
